$.tablesorter.addParser({ 
id: 'image', 
is: function(s) { 
	return false; 
}, 
format: function(s) {
	return s.match( /<img.*src=\"(.*?)\".*?\>(.*)+/)[2];
}, 
type: 'text' 
}); 
$.tablesorter.addParser({ 
id: 'item', 
is: function(s) { 
	return false; 
}, 
format: function(s) {
	return s.match( /<a.*href=\"(.*?)\".*?\>(.*)\<\/a\>+/)[2];
}, 
type: 'text' 
}); 
$.tablesorter.addParser({ 
id: 'weekday', 
is: function(s) { 
	return false; 
}, 
format: function(s) {
	var val = 0;
	
	switch (s) {
		case ' Sunday ': val =  1; break;
		case ' Monday ': val = 2; break;
		case ' Tuesday ': val = 3; break;
		case ' Wednesday ': val = 4; break;
		case ' Thursday ': val = 5; break;
		case ' Friday ': val = 6; break;
		case ' Saturday ': val = 7; break;
	}
	return val;
}, 
type: 'numeric' 
}); 
$.tablesorter.addParser({ 
id: 'gold', 
is: function(s) { 
	return false; 
}, 
format: function(s) {
	var val = 0; var neg = 0;
	if (s[0] == '-') { neg = 1; }
	var tmp = s.match( /(\d*)<img.*gold.png">/ ); if (tmp) { val += (tmp[1] *  10000); s = s.replace(tmp[0],''); }
	var tmp = s.match( /(\d*)<img.*silver.png">/ );	if (tmp) { val += (tmp[1] *  100); s = s.replace(tmp[0],''); }
	var tmp = s.match( /(\d*)<img.*copper/ );	if (tmp) { val += (tmp[1] * 1); }
	if (neg == 1) { val = val * -1; }
	return val;
}, 
type: 'integer' 
});
var ajax_load = "<img src='images/indicator.gif' alt='loading...' />"; 
var ajax_lg_load = "<div style='width: 900px; text-align: center;'><img src='images/indicator.gif' style='margin-left: auto; margin-right: auto' alt='loading...' />";
ajax_lg_load += "<br />Please wait while the page loads</div>";
var content = "";
function log(message) {
	$("<div/>").text(message).prependTo("#log");
	$("#log").attr("scrollTop", 0);
}
function fadeToggle(obj) {
	$(obj).animate({"height": "toggle", "opacity": "toggle"}, { duration: "slow" });
}
function getGold(num) {
	var result = "";
	var g = Math.floor(num); num = (num - g) * 100;
	var s = Math.floor(num); num = (num - s) * 100;
	var c = Math.round(num);
	result = c + '<img src="images/copper.png" /> ';
	if (s || g) { result = s + '<img src="images/silver.png" /> ' + result; }
	if (g) { result = addCommas(g) + '<img src="images/gold.png" /> ' + result; }
	return result;
}
function showAuc(limit,id) {
	if ( id === undefined ) {
		var ajaxurl = "ajax/auctionlist.php?limit=" + limit;
	} else {
		var ajaxurl = "ajax/auctionlist.php?limit=" + limit + "&id=" + id;
	}
	ajaxload("#content", ajaxurl);
}
function setServer(id) {
	$.cookie("server", id, { expires: 7 });
	reload();
}
function showSales(limit, id) {
	if ( id === undefined ) {
		var ajaxurl = "ajax/saleslist.php?limit=" + limit;
	} else {
		var ajaxurl = "ajax/itemsales.php?limit=" + limit + "&id=" + id;
	}
	ajaxload("#content", ajaxurl);
}
function showPurchases(limit, id) {
	if ( id === undefined ) {
		var ajaxurl = "ajax/purchaselist.php?limit=" + limit;
	} else {
		var ajaxurl = "ajax/purchaselist.php?limit=" + limit + "&id=" + id;
	}
	ajaxload("#content", ajaxurl);
}
function showScans(limit, id) {
	if ( id === undefined ) {
		return false;
	} else {
		var ajaxurl = "ajax/itemscans.php?limit=" + limit + "&id=" + id;
	}
	ajaxload("#content", ajaxurl);
}
function showRecipe(id) {
	var ajaxurl = "ajax/recipe.php?r=" + id;
	ajaxload("#content", ajaxurl);
}
function ajaxload(container, link) {
	$.cookie("contentURL", link);
	var url = link.split("?");
	$(container).html(ajax_lg_load).load(url[0], url[1]);
}
function reload() {
	$("#headerSales").html(ajax_load).load("ajax/header.php");  
	$("#wealth").html(ajax_load).load("ajax/wealth.php");
	$("#top10salesdates").html(ajax_load).load("ajax/stats.php");
	ajaxload("#content", $.cookie("contentURL"));
}
function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}